package com.framework.boc.admin.sms;

import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;

public class SmsCode {


    private String phoneNum;

    private String code;

    private LocalDateTime expireTime;


    /**
     * 构造函数
     * @param expireSeconds     过期时长
     */
    public SmsCode(String phoneNum, String code, long expireSeconds) {
        this.phoneNum = phoneNum;
        this.code = code;
        this.expireTime = LocalDateTime.now().plusSeconds(expireSeconds);
    }

    /**
     * 获取电话号码
     * @return
     */
    public String getPhoneNum() {
        return phoneNum;
    }

    /**
     * 获取短信验证码
     * @return
     */
    public String getCode() {
        return code;
    }

    /**
     * 检测验证码是否过期
     * @return      true：过期     false：未过期
     */
    public boolean isExpire() {
        return LocalDateTime.now().isAfter(expireTime);
    }


    /**
     * 检测验证码
     * @param code      输入的验证码
     * @throws SmsCodeCheckException    验证码异常
     */
    public void checkSmsCode(String code) throws SmsCodeCheckException {
        if (isExpire()) {
            throw new SmsCodeCheckException("验证码已经过期");
        }
        if (!StringUtils.equals(code, this.code)) {
            throw new SmsCodeCheckException("验证码不正确");
        }
    }


}
